/*-----------------------------------------------------------------------*/
/* Low level disk I/O module skeleton for FatFs     (C)ChaN, 2007        */
/*-----------------------------------------------------------------------*/
/* This is a stub disk I/O module that acts as front end of the existing */
/* disk I/O modules and attach it to FatFs module with common interface. */
/*-----------------------------------------------------------------------*/

#include "diskio.h"
#include "driver_ide.h"

/*-----------------------------------------------------------------------*/
/* Correspondence between physical drive number and physical drive.      */


/*-----------------------------------------------------------------------*/
/* Inidialize a Drive                                                    */

DSTATUS disk_initialize (
	BYTE drv				/* Physical drive nmuber (0..) */
)
{
  return IDE_INIT();
}



/*-----------------------------------------------------------------------*/
/* Return Disk Status                                                    */

DSTATUS disk_status (
	BYTE drv		/* Physical drive nmuber (0..) */
)
{
   while(BSY);
	return RES_OK;
}



/*-----------------------------------------------------------------------*/
/* Read Sector(s)                                                        */

DRESULT disk_read (
	BYTE drv,		/* Physical drive nmuber (0..) */
	BYTE *buff,		/* Data buffer to store read data */
	DWORD sector,	/* Sector address (LBA) */
	BYTE count		/* Number of sectors to read (1..255) */
)
{
    unsigned short *p;
	unsigned int temp;
	p = (unsigned short *)buff;
 
    while(BSY);ATA_REG_DEVICE_HEAD = (unsigned char)(0xe0+(sector>>24));
    while(BSY);ATA_REG_CYLI_HIGH   = (unsigned char)(sector>>16);
    while(BSY);ATA_REG_CYLI_LOW    = (unsigned char)(sector>>8);
    while(BSY);ATA_REG_SECTOR      = (unsigned char)sector;
    while(BSY);ATA_REG_SECCNT      = (unsigned char)count;

 do{ATA_REG_COMMAND = CMD_READ_SECTORS;}while(DRQ || BSY);
 for(temp=0;temp<(256*count);temp++)
 {
  *p = ATA_REG_DATA;
  p++;
  while(DRQ || BSY);
 }
	return RES_OK;
}



/*-----------------------------------------------------------------------*/
/* Write Sector(s)                                                       */

#if _READONLY == 0
DRESULT disk_write (
	BYTE drv,			/* Physical drive nmuber (0..) */
	const BYTE *buff,	/* Data to be written */
	DWORD sector,		/* Sector address (LBA) */
	BYTE count			/* Number of sectors to write (1..255) */
)
{
	return RES_PARERR;
}
#endif /* _READONLY */



/*-----------------------------------------------------------------------*/
/* Miscellaneous Functions                                               */

DRESULT disk_ioctl (
	BYTE drv,		/* Physical drive nmuber (0..) */
	BYTE ctrl,		/* Control code */
	void *buff		/* Buffer to send/receive control data */
)
{
	return RES_OK;
}

DWORD get_fattime (void)
{
 return 0;
}

